<template>
  <el-form label-width="100px">
    <el-form-item label="SKU名称">
      <el-input placeholder="SKU名称" v-model="skuParams.skuName"></el-input>
    </el-form-item>
    <el-form-item label="价格(元)">
      <el-input
        placeholder="价格(元)"
        type="number"
        v-model="skuParams.price"
      ></el-input>
    </el-form-item>
    <el-form-item label="重量(g)">
      <el-input
        placeholder="重量(g)"
        type="number"
        v-model="skuParams.weight"
      ></el-input>
    </el-form-item>
    <el-form-item label="SKU描述">
      <el-input
        placeholder="SKU描述"
        type="textarea"
        v-model="skuParams.skuDesc"
      ></el-input>
    </el-form-item>
    <el-form-item label="平台属性">
      <el-form :inline="true" label-width="100px">
        <el-form-item
          :label="item.attrName"
          v-for="item in attrArr"
          :key="item.id"
          style="margin-bottom: 5px"
        >
          <el-select v-model="item.attrIdAndValueId">
            <el-option
              :value="`${item.id}:${attrValue.id}`"
              :label="attrValue.valueName"
              v-for="attrValue in item.attrValueList"
              :key="attrValue.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="销售属性">
      <el-form :inline="true">
        <el-form-item
          :label="item.saleAttrName"
          v-for="item in saleArr"
          :key="item.id"
        >
          <el-select v-model="item.saleIdAndValueId">
            <el-option
              :value="`${item.id}:${saleAttrValue.id}`"
              :label="saleAttrValue.saleAttrValueName"
              v-for="saleAttrValue in item.spuSaleAttrValueList"
              :key="saleAttrValue.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-form-item>
    <el-form-item label="图片名称">
      <el-table border :data="imgArr" ref="table">
        <el-table-column
          type="selection"
          width="80px"
          align="center"
        ></el-table-column>
        <el-table-column label="图片">
          <template #default="scope">
            <img
              :src="scope.row.imgUrl"
              alt=""
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
        <el-table-column label="名称" prop="imgName"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handler(scope.row)">
              设置默认
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="default" @click="save">保存</el-button>
      <el-button size="default" @click="cancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { reqAttr } from "../../../../api/product/attr/index";
import {
  reqSpuImageList,
  reqSpuHasSaleAttr,
  reqAddSku,
} from "../../../../api/product/spu/index";
import { ref, reactive } from "vue";
import type { SkuData } from "../../../../api/product/spu/type";
import { ElMessage } from "element-plus";

let $emit = defineEmits(["changeScene"]);
let attrArr = ref<any>([]);
let saleArr = ref<any>([]);
let imgArr = ref<any>([]);
let table = ref<any>();

let skuParams = reactive<SkuData>({
  category3Id: "", // 三级分类的ID
  spuId: "", // spu的ID
  tmId: "", // spu品牌的ID
  skuName: "", // sku名称
  price: "", // sku价格
  weight: "", // sku重量
  skuDesc: "", // sku的描述
  skuAttrValueList: [
    // 平台属性的收集
    // {
    //   attrId: '',
    //   valueId: '',
    // },
  ],
  skuSaleAttrValueList: [
    // {
    //   saleAttrId: '',
    //   saleAttrValueId: '',
    // },
  ],
  skuDefaultImg: "", // sku图片地址
});
// 子组件点击后改变父组件场景值
const cancel = () => {
  $emit("changeScene", { flag: 0, params: "" });
};
// 当前子组件的方法对外暴露
const initSkuData = async (
  c1Id: number | string,
  c2Id: number | string,
  spu: any,
) => {
  skuParams.category3Id = spu.category3Id;
  skuParams.spuId = spu.id;
  skuParams.tmId = spu.tmId;
  // 获取平台属性
  let res: any = await reqAttr(c1Id, c2Id, spu.category3Id);
  // 获取对应的销售属性
  let res1: any = await reqSpuHasSaleAttr(spu.id);
  // 获取照片墙的数据
  let res2: any = await reqSpuImageList(spu.id);
  // 平台属性
  attrArr.value = res.data;
  // 销售属性
  saleArr.value = res1.data;
  // 照片墙数据
  imgArr.value = res2.data;
};

const handler = (row: any) => {
  imgArr.value.forEach((item: any) => {
    table.value.toggleRowSelection(item, false);
  });
  table.value.toggleRowSelection(row, true);
  skuParams.skuDefaultImg = row.imgUrl;
};

const save = async () => {
  // 平台属性
  skuParams.skuAttrValueList = attrArr.value.reduce((prev: any, next: any) => {
    if (next.attrIdAndValueId) {
      let [attrId, valueId] = next.attrIdAndValueId.split(":");
      prev.push({
        attrId,
        valueId,
      });
    }
    return prev;
  }, []);
  // 销售属性
  skuParams.skuSaleAttrValueList = saleArr.value.reduce(
    (prev: any, next: any) => {
      if (next.saleIdAndValueId) {
        let [saleAttrId, saleAttrValueId] = next.saleIdAndValueId.split(":");
        prev.push({
          saleAttrId,
          saleAttrValueId,
        });
      }
      return prev;
    },
    [],
  );
  // 发送请求
  let res: any = await reqAddSku(skuParams);
  // 成功后的操作
  if (res.code === 200) {
    ElMessage({
      type: "success",
      message: "添加SKU成功",
    });
    $emit("changeScene", { flag: 0, params: "" });
  } else {
    // 请求失败的操作
    ElMessage({
      type: "error",
      message: "添加SKU失败",
    });
  }
};
// 导出给父组件使用
defineExpose({
  initSkuData,
});
</script>
<style lang="scss" scoped></style>
